package org.itboys.core.service.mysql.front;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.dao.score.SundayScoreRecordMapper;
import org.itboys.core.entity.mysql.SundayScore;
import org.itboys.core.entity.mysql.SundayScoreRecord;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/21
 * Description:
 */

@Service
public class SundayFrontScoreService extends BaseMySqlService<SundayScore> {
    @Autowired
    private SundayScoreRecordMapper recordMapper;

    /**
     * 获取积分主表
     *
     * @param memberId
     * @return
     */
    public SundayScore getScore(Long memberId) {
        Map<String, Object> param = new HashMap<>();
        param.put("memberId", memberId);
        SundayScore       score  = null;
        List<SundayScore> scores = super.select(param);
        if (ListUtil.isNotNull(scores)) {
            score = scores.get(0);
        } else {
            score = new SundayScore(memberId);
            score.setAbleScore(new BigDecimal("0"));
            score.setTotalScore(new BigDecimal("0"));
            super.insert(score);
        }
        return score;
    }

    /**
     * 分页获取用户积分记录
     *
     * @param request
     * @param memberId
     * @return
     */
    public List<SundayScoreRecord> selectScoreRecord(HttpServletRequest request, Long memberId) {
        Map<String, Object> param = ParamUtil.packageMysqlPageParam(request, "remark");
        param.put("memberId", memberId);
        List<SundayScoreRecord> records = recordMapper.select(param);
        return records;
    }


}
